package dp.mobile.game;

import android.os.Handler;
import android.util.Log;
import dp.mobile.eden.CampaignAct;
import dp.mobile.entity.generator.Generator;
import dp.mobile.entity.tower.Tower;

public class TutorialGame extends Game {
	private boolean[] mActionDone = new boolean[9];
	private boolean mFirst = true;
	protected int mCurrent;
	
	public TutorialGame(String pLevelSource) {
		super(pLevelSource);
		for (boolean iAction : mActionDone)
			iAction = false;
		getPlayer().setSelectedTile(3, 4);
		mCurrent = -1;
	}
	
	private int getFirstUndoneAction() {
		for (int i = 0 ; i < mActionDone.length ; i++) {
			if (!mActionDone[i]) return i;
		}
		return 0;
	}
	
	private void markDone(int pIndex) {
		mActionDone[pIndex] = true;
	}
	
	@Override
	protected void afterShowPanel() {
		int tFirstUndone = getFirstUndoneAction();
		if (tFirstUndone == 2) {
			proceed(2);
		}
	}
	
	@Override
	protected void afterAddGenerator(Generator g) {
		super.afterAddGenerator(g);
		int tFirstUndone = getFirstUndoneAction();
		if (tFirstUndone == 3)
			proceed(3, true);
	}
	
	@Override
	protected void afterUpdate() {
		super.afterUpdate();
		
		int tFirstUndone = getFirstUndoneAction();
		android.os.Message tMessage = new android.os.Message();
		
		if (tFirstUndone == 4 && mPlayer.getElectricity() >= 10) {
			tMessage.what = 4;
		}
		else if (tFirstUndone == 8 && mPlayer.getComponents(0) == 2) {
			///proceed(6);
			tMessage.what = 8;
		}
		else if (tFirstUndone == 1 && mFirst) {
			tMessage.what = 1;
		} else if (tFirstUndone == 6) {
			tMessage.what = 6;
		}
		
		mTutorialHandler.sendMessage(tMessage);
	}
	
	final Handler mTutorialHandler = new Handler() {
		
		@Override
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 4) {
				proceed(4);
				getPlayer().setSelectedTile(9, 6);
			}
			else if (msg.what == 1) {
				proceed(1);
			}
			else if (msg.what == 2) {
				proceed(2);
			}
			else if (msg.what == 6) {
				proceed(6);
			}
		};
	};
	
	@Override
	protected void afterDismantleTower(Tower t) {
		super.afterDismantleTower(t);
		int tFirstUndone = getFirstUndoneAction();
		if (tFirstUndone == 7)
			proceed(7, true);
	}
	
	private void proceed(int pIndex) {
		proceed(pIndex, false);
	}
	
	private void proceed(int pIndex, boolean dialog) {
		//Skip if double
		if (mCurrent == pIndex) return;
		
		//Set as current
		mCurrent = pIndex;
		
		//If on campaign
		if (mListener instanceof CampaignAct) {
			//Get listener
			CampaignAct tTarget = (CampaignAct) mListener;
			
			if (dialog) {
				checkDialog(pIndex);
				tTarget.hideTutorialPanel();
			}
			else tTarget.showTutorialPanel(mLevel.getDialog(pIndex).getDialogs().get(0).get(0).getText());
		}
		
		//Done
		markDone(pIndex);
	}
	
	@Override
	public void checkDialog() {
		int tFirstUndone = getFirstUndoneAction();
		if (mPlayer.getWave() == 1 && tFirstUndone == 5) {
			checkDialog(5);
			markDone(5);
		}
		else if (mPlayer.getWave() == 0 && tFirstUndone == 0) {
			checkDialog(0);
			markDone(0);
		}
	}
}
